import re

from flask import request
from flask_restful import Resource, reqparse

from flask_shop import models, db
from flask_shop.user import user_bp, user_api

from flask_shop.utils.token import generate_token, verify_token, login_require


# 创建视图
@user_bp.route('/')
def index():
    return "user页面"


# 登录功能
@user_bp.route('/login/', methods=['POST'])
def login():
    name = request.get_json().get('name')
    pwd = request.get_json().get('pwd')
    if not all([name, pwd]):
        return {'status': 400, 'msg': '参数不完整'}
    else:
        user = models.User.query.filter(name == name).first()
        if user:
            if user.check_password(pwd):
                # 生成token
                token = generate_token({'id': user.id})
                return {'status': 200, 'msg': '登录成功', 'data': {'token': token}}
        return {'status': 400, 'msg': '用户名或密码错误'}


class Users(Resource):
    def get(self):
        # 创建RequestParser
        parser = reqparse.RequestParser()
        # 增加参数
        parser.add_argument('pnum', type=int, default=1, location='args')
        parser.add_argument('psize', type=int, default=10, location='args')
        parser.add_argument('name', type=str, location='args')
        # 解析数据
        args = parser.parse_args()

        name = args.get('name')
        pnum = args.get('pnum')
        psize = args.get('psize')

        # 根据参数进行查询
        if name:
            user_list = models.User.query.filter(models.User.name.like(f"%{name}%")).paginate(page=pnum, per_page=psize)
        else:
            user_list = models.User.query.paginate(page=pnum, per_page=psize)
        data = {
            'total': user_list.total,
            'pnum': pnum,
            'data': [u.to_dict() for u in user_list.items]
        }
        return {'status': 200, 'msg': "获取用户列表成功", 'data': data}

    def post(self):
        # 注册用户
        name = request.get_json().get('name')
        pwd = request.get_json().get('pwd')
        real_pwd = request.get_json().get('real_pwd')
        # 验证数据合法性
        if not all([name, pwd, real_pwd]):
            return {'status': 400, 'msg': '参数不完整'}
        # 判断两次密码是否一致
        if pwd != real_pwd:
            return {'status': 400, 'msg': '两次密码不一致'}
        # 判断用户名是否合法
        if len(name) < 2:
            return {'status': 400, 'msg': '用户名不合法'}
        # 接收手机号和邮箱
        phone = request.get_json().get('phone')
        email = request.get_json().get('email')
        # 昵称
        nick_name = request.get_json().get('nickname')
        if not re.match(r'^1[3456789]\d{9}$', phone):
            return {'status': 400, 'msg': '手机号不合法'}
        if not re.match('^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$', email):
            return {'status': 400, 'msg': '邮箱不合法'}
        # 接受角色ID信息
        role_id = request.get_json().get('role_id')
        try:
            # 判断用户名是否存在
            user = models.User.query.filter(models.User.name == name).first()
            if user:
                return {'status': 400, 'msg': '用户名已存在'}
        except Exception as e:
            print(e)
        if role_id:
            user = models.User(name=name, password=pwd, phone=phone, email=email, nick_name=nick_name, role_id=role_id)
        else:
            user = models.User(name=name, password=pwd, phone=phone, email=email, nick_name=nick_name)
        # 保存数据库
        db.session.add(user)
        db.session.commit()
        return {'status': 200, 'msg': '注册成功'}


user_api.add_resource(Users, '/users/')


class User(Resource):
    def get(self, id):
        user = models.User.query.get(id)
        if user:
            return {'status': 200, 'msg': '查询成功', 'data': user.to_dict()}
        else:
            return {'status': 404, 'msg': '用户不存在'}

    def put(self, id):
        user = models.User.query.get(id)
        parser = reqparse.RequestParser()
        # 增加参数
        parser.add_argument('nickname', type=str)
        parser.add_argument('phone', type=str)
        parser.add_argument('email', type=str)
        parser.add_argument('role_id', type=int)
        args = parser.parse_args()
        if args.get('nickname'):
            user.nick_name = args.get('nickname')
        if args.get('phone'):
            user.phone = args.get('phone')
        if args.get('email'):
            user.email = args.get('email')
        if args.get('role_id'):
            user.role_id = args.get('role_id')
        db.session.commit()
        return {'status': 200, 'msg': '修改成功', 'data': user.to_dict()}

    def delete(self, id):
        try:
            user = models.User.query.get(id)
            if user:
                db.session.delete(user)
                db.session.commit()
            return {'status': 200, 'msg': '删除成功'}
        except Exception as e:
            print(e)
            return {'status': 400, 'msg': '删除失败'}


user_api.add_resource(User, '/user/<int:id>/')


@user_bp.route('/rest_pwd/<int:id>/')
def rest_pwd(id: int):
    try:
        user = models.User.query.get(id)
        user.password = '123456'
        db.session.commit()
        return {'status': 200, 'msg': '重置密码成功'}
    except Exception as e:
        print(e)
        return {'status': 400, 'msg': '重置密码失败'}


@user_bp.route('/test/', methods=['GET'])
@login_require
def test_login_required():
    return {'status': 200, 'msg': '验证成功'}
